package Lagou.PriorityQueue03;

import java.util.PriorityQueue;
import java.util.Queue;

public class T0302 {
    //用语言内置的优先级队列来解决
    public int[] getLeastNumbers(int[] arr, int k) {
        if (k <= 0 || arr == null || arr.length == 0) {
            return new int[0];
        }
        Queue<Integer> Q = new PriorityQueue<>((v1, v2) -> v2 - v1);
        for (int x : arr){
            Q.offer(x);
            while (Q.size() > k) {
                Q.poll();
            }
        }

        //生成返回结果
        int[] ans = new int[k];
        int i = 0;
        for (int x: Q) {
            ans[i++] = x;
        }
        return ans;
    }
}
